package com.dyz.swordFingerOffer;

import java.util.ArrayList;

public class FindNumbersWithSum57 {
    public static void main(String[] args) {
        int []array = new int[]{1,2,4,7,11,15};
        new FindNumbersWithSum57().FindNumbersWithSum(array, 15);
    }
    public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
        ArrayList<ArrayList<Integer>> arr2 = new ArrayList<ArrayList<Integer>>();

        if(sum==0){
            return new ArrayList<>();
        }

        for(int i = 0; i<array.length; i++){
            if(contains(array, sum-array[i])){
                ArrayList<Integer> arr1 = new ArrayList<>();
                arr1.add(Math.min(array[i],  sum-array[i]));
                arr1.add(Math.max(array[i],  sum-array[i]));
                arr2.add(arr1);
            }
        }
        int mul = sum*sum;
        ArrayList<Integer> arr = new ArrayList<>();
        for(ArrayList<Integer> a: arr2){
            int temp = a.get(0)*a.get(1);
            if(mul>temp){
                mul = temp;
                arr = a;
            }
        }

        return arr;
    }
    private boolean contains(int [] array, int sum){
        boolean flag = false;
        for (int a:array){
            if(a == sum){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
